import random
import time

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_personal_role_locator import PersonalRoleLocator


class PersonalRole(Basepage):

    def enter_personal_role_page(self):
        doc = "进入Personal User-Role页面"
        for i in range(3):
            try:
                self.wait_elevisble(PersonalRoleLocator.personal_user_tab, doc)
                self.click_element(PersonalRoleLocator.personal_user_tab, doc)
                self.wait_elevisble(PersonalRoleLocator.role_tab, doc)
                self.click_element(PersonalRoleLocator.role_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        self.wait_elevisble(PersonalRoleLocator.page_total, doc)

    def query_role(self, role_name):
        doc = "查询角色"
        self.enter_personal_role_page()
        self.wait_elevisble(PersonalRoleLocator.query_input, doc)
        self.keyboard_event(PersonalRoleLocator.query_input, doc)
        time.sleep(2)
        self.get_element(PersonalRoleLocator.query_input, doc).send_keys(Keys.CONTROL, "a")
        self.get_element(PersonalRoleLocator.query_input, doc).send_keys(Keys.BACKSPACE)
        self.send_keys(PersonalRoleLocator.query_input, role_name, doc)
        self.move_mouse_to_click_element(PersonalRoleLocator.query_button, doc)
        time.sleep(2)
        assert len(self.get_elements(PersonalRoleLocator.query_results, doc)) > 0

    def query_nonexistent_role(self, role_name):
        self.refresh_page()
        doc = "查询不存在角色"
        self.wait_elevisble(PersonalRoleLocator.query_input, doc)
        self.keyboard_event(PersonalRoleLocator.query_input, doc)
        time.sleep(2)
        self.send_keys(PersonalRoleLocator.query_input, role_name, doc)
        self.move_mouse_to_click_element(PersonalRoleLocator.query_button, doc)
        time.sleep(2)
        assert len(self.get_elements(PersonalRoleLocator.query_results, doc)) == 0

    def list_columns(self):
        doc = "角色列表字段校验"

        self.wait_eleinvisble(PersonalRoleLocator.loading_wait, doc)

        time.sleep(5)
        list_columns = self.get_elements(PersonalRoleLocator.list_columns, doc)
        assert list_columns[0].text == "Role Name"
        assert list_columns[1].text == "Customer"
        assert list_columns[2].text == "Role Type"
        assert list_columns[3].text.startswith("Creation Time")
        assert list_columns[4].text == "Actions"

        edit_ele = self.get_element(PersonalRoleLocator.list_action_edit, doc).find_element(By.XPATH, "./span")
        assert edit_ele.text == "Edit"

        self.wait_elevisble(PersonalRoleLocator.list_action_ellipsis, doc)
        self.move_mouse_to_element(PersonalRoleLocator.list_action_ellipsis)

        time.sleep(2)
        self.wait_elevisble(PersonalRoleLocator.list_actions_expand, doc)
        assert self.get_elements(PersonalRoleLocator.list_actions_expand, doc)[0].find_element(By.XPATH,
                                                                                               "./span").text == "Edit Role Permissions"
        assert self.get_elements(PersonalRoleLocator.list_actions_expand, doc)[1].find_element(By.XPATH,
                                                                                               "./span").text == "Delete"

    def role_check(self):
        doc = "查看角色信息"
        time.sleep(3)
        self.click_element(PersonalRoleLocator.list_column_role_name, doc)

        self.wait_elevisble(PersonalRoleLocator.role_info_tabs, doc)
        time.sleep(3)
        # overview tab下的条目
        overview_items = self.get_elements(PersonalRoleLocator.role_info_items, doc)
        assert overview_items[0].text == "Overview"
        assert overview_items[1].text == "Role Allocation"

        self.get_elements(PersonalRoleLocator.role_info_tabs, doc)[1].click()
        time.sleep(2)
        role_permissions_items = self.get_elements(PersonalRoleLocator.role_info_items, doc)
        assert role_permissions_items[0].text == "Role Permissions"

    def role_allocate_list_query(self, email):
        doc = "查询角色分配列表"
        time.sleep(10)
        self.scroll_bar_operation(PersonalRoleLocator.role_allocate_list_page_total)

        self.send_keys(PersonalRoleLocator.role_allocate_query_input, email, doc)
        self.click_element(PersonalRoleLocator.role_allocate_query_button, doc)

    def role_allocate_list_columns(self, email):
        doc = "角色分配列表字段校验"
        self.click_element(PersonalRoleLocator.list_column_role_name, doc)

        self.wait_elevisble(PersonalRoleLocator.role_info_tabs, doc)
        self.role_allocate_list_query(email)
        time.sleep(3)

        self.wait_elevisble(PersonalRoleLocator.role_allocate_list_page_total, doc)

        list_columns = self.get_elements(PersonalRoleLocator.role_allocate_list_columns, doc)
        assert list_columns[0].text == "Account Email"
        assert list_columns[1].text == "User Name"
        assert list_columns[2].text == "Phone Number"
        assert list_columns[3].text.startswith("Creation Time")
        assert list_columns[4].text == "Actions" or list_columns[5].text == "Actions"

        buttons = self.get_elements(PersonalRoleLocator.role_allocate_list_buttons, doc)

        assert buttons[0].text == "Reset Password"
        assert buttons[1].text == "Lock"

        self.move_mouse_to_element(PersonalRoleLocator.role_allocate_list_actions_expand)
        time.sleep(2)
        self.wait_elevisble(PersonalRoleLocator.role_allocate_list_action_unlock, doc)
        assert self.get_element(PersonalRoleLocator.role_allocate_list_action_unlock, doc).text == "Unlock"
